package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.dto.*;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘岩
 * Date: 2020-10-01
 * Time: 21:04
 *
 * @version 1.0
 */
@RestController
@RequestMapping("/programmer")
@Api(tags = "程序员模块个人信息相关Api")
public class ProgrammerController {

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:保存个人资料接口
     *
     * @param programmer
     * @return
     */
    @ApiOperation(value = "保存个人资料接口", notes = "保存个人资料")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "nickName", value = "昵称", required = true, dataType = "string"),
            @ApiImplicitParam(name = "programmerStatus", value = "程序员状态", dataType = "string"),
            @ApiImplicitParam(name = "programmerCareer", value = "职业", dataType = "string"),
            @ApiImplicitParam(name = "programmerCareerDirect", value = "职业方向", dataType = "string"),
            @ApiImplicitParam(name = "programmerCompany", value = "公司", dataType = "string"),
            @ApiImplicitParam(name = "programmerPosition", value = "职位", dataType = "string"),
            @ApiImplicitParam(name = "programmerEmail", value = "邮箱", dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })

    @PutMapping("/personalInfo")
    public Map<String, Object> savePersonalInfo(HttpServletRequest request, @RequestBody Programmer programmer) {
        Long myId = RequestIdUtil.getMyId(request);
        programmer.setProgrammerId(myId);
        boolean flag = programmerService.savePersonalInfo(programmer);
        if (flag)
            return ResponseData.REP_CREATED_SUCCESS();
        else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:保存个人账号接口
     *
     * @param programmer
     * @return
     */
    @ApiOperation(value = "保存个人账号接口", notes = "保存账号信息")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "alipayAccount", value = "支付宝账号", dataType = "string"),
            @ApiImplicitParam(name = "wechatAccount", value = "微信账号", dataType = "string"),
            @ApiImplicitParam(name = "qqAccount", value = "qq账号", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })

    @PutMapping("/accountInfo")
    public Map<String, Object> saveAccountInfo(HttpServletRequest request, @RequestBody Programmer programmer) {
        Long myId = RequestIdUtil.getMyId(request);
        programmer.setProgrammerId(myId);
        boolean flag = programmerService.saveAccountInfo(programmer);
        if (flag)
            return ResponseData.REP_CREATED_SUCCESS();
        else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:修改个人密码接口
     *
     * @param programmerDTO
     * @return
     */
    @ApiOperation(value = "修改个人密码接口", notes = "修改个人密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "programmerId", value = "程序员id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "originalPassword", value = "原密码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "修改成功"),
            @ApiResponse(code = 400, message = "修改失败"),
    })

    @PutMapping("/passwordInfo")
    public Map<String, Object> updatePassword(HttpServletRequest request, @RequestBody ProgrammerDTO programmerDTO) {
        Long myId = RequestIdUtil.getMyId(request);
        programmerDTO.setProgrammerId(myId);
        boolean flag = programmerService.updatePassword(programmerDTO);
        if (flag)
            return ResponseData.REP_CREATED_SUCCESS();
        else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * 保存个人简介接口
     *
     * @param programmer
     * @return
     */
    @ApiOperation(value = "保存个人简介接口", notes = "保存个人简介")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "programmerIntroduction", required = true, value = "程序员简介", dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })


    @PutMapping("/perIntroduce")
    public Map<String, Object> savePerIntroduce(@RequestBody Programmer programmer, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        programmer.setProgrammerId(myId);
        boolean flag = programmerService.savePerIntroduce(programmer);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:查询个人资料接口
     *
     * @param programmerId
     * @return
     */
    @ApiOperation(value = "查询个人资料接口", notes = "查询个人资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "programmerId", value = "程序员id", required = true, dataType = "long")

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/programmerInfo/{programmerId}")
    public Map<String, Object> getProgrammerInfo(@PathVariable("programmerId") long programmerId) {
        Programmer programmerInfo = programmerService.getProgrammerInfo(programmerId);
        return ResponseData.REP_GET_SUCCESS(programmerInfo);

    }


    /**
     * desc:实名认证接口
     *
     * @param programmerId
     * @return
     */
    @ApiOperation(value = "实名认证查看账号接口", notes = "实名认证查看账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "programmerId", value = "程序员id", required = true, dataType = "long")

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/programmerAccount/{programmerId}")
    public Map<String, Object> getProgrammerAccount(@PathVariable("programmerId") long programmerId) {
        ProgrammerAccountDTO programmerAccount = programmerService.getProgrammerAccount(programmerId);
        return ResponseData.REP_GET_SUCCESS(programmerAccount);

    }

    /**
     * desc:查询个人简简介接口
     *
     * @param programmerId
     * @return
     */
    @ApiOperation(value = "查询个人简简介接口", notes = "用户查询个人简介")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "programmerId", value = "程序员id", required = true, dataType = "long")

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/perIntroduce/{programmerId}")
    public Map<String, Object> getPerIntroduce(@PathVariable("programmerId") long programmerId) {
        PerIntroduceDTO perIntroduce = programmerService.getPerIntroduce(programmerId);
        return ResponseData.REP_GET_SUCCESS(perIntroduce);
    }


    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public Map<String, Object> search(
            @PathVariable("keyword") String keyword,
            @PathVariable("pageNo") int pageNo,
            @PathVariable("pageSize") int pageSize) throws Exception {
        // 获取数据实现搜索功能

        List<Map<String, Object>> list = programmerService.searchPage(keyword, pageNo, pageSize);
        return ResponseData.REP_GET_SUCCESS(list);
    }

    /**
     * desc:查询首页最新经典评价
     */
    @ApiOperation(value = "查询首页最新经典评价接口", notes = "查询首页最新经典评价")
    @ApiImplicitParams({

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/programmers")
    public Map<String, Object> getProgrammers() {
        List<ProgrammerHomeDTO> programmers = programmerService.getProgrammers();
        return ResponseData.REP_GET_SUCCESS(programmers);
    }

    /**
     * desc:查询相似推荐
     */
    @ApiOperation(value = "查询程序员首页查询相似推荐接口", notes = "查询程序员首页查询相似推荐")
    @ApiImplicitParams({

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })

    @GetMapping("/recommend/{userCareer}/{userId}")
    public Map<String, Object> getRecommend(@PathVariable("userCareer") String userCareer, @PathVariable("userId") Integer userId) {
        List<RecommendProgrammer> recommend = programmerService.getRecommend(userCareer, userId);
        return ResponseData.REP_GET_SUCCESS(recommend);
    }


}
